using QFramework;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using WuLibrary;

namespace TowerDefence_QF
{
    public class EnemySystem : AbstractSystem
    {
        /// <summary>
        /// 敌人对象池字典
        /// </summary>
        private Dictionary<string, SimpleObjectPool<EnemyController>> mEnemyPoolDic;
        /// <summary>
        /// 敌人预制体字典
        /// </summary>
        private Dictionary<string,GameObject> mEnemyPrefabDic;
        /// <summary>
        /// 敌人预制体路径
        /// </summary>
        private const string ENEMY_PREFAB_PATH = "EnemyPrefabs";

        private const string ENEMY_DATAS_PATHS = "EnemyDatas";

        private EnemyDatas mEnemyDatas;

        private ResLoader mResLoader;

        protected override void OnInit()
        {
            //获取一个加载器
            mResLoader=ResLoader.Allocate();

            mEnemyPoolDic = new Dictionary<string, SimpleObjectPool<EnemyController>>();
            InitEnemyDatas();
            InitEnemyPrefabDic();
        }

        private void InitEnemyDatas()
        {
            mEnemyDatas = mResLoader.LoadSync<EnemyDatas>(ENEMY_DATAS_PATHS);
        }
       /// <summary>
       /// 初始化敌人预制体字典
       /// </summary>
       private void InitEnemyPrefabDic()
       {
            mEnemyPrefabDic = new Dictionary<string, GameObject>();

            //加载ab包
            var ab = mResLoader.LoadSync<AssetBundle>(ENEMY_PREFAB_PATH);
            //加载所有的预制体
            var enemyPrefabs=ab.LoadAllAssets<GameObject>();
            //将预制体加入字典
            foreach(var go in enemyPrefabs)
            { 
                if(!mEnemyPrefabDic.ContainsKey(go.name))
                {
                    mEnemyPrefabDic.Add(go.name, go);
                }
            }
       }
        /// <summary>
        /// 根据名字获取对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public EnemyController Allocate(string name)
        {
            EnemyController enemyController;
            if(!mEnemyPoolDic.ContainsKey(name))
            {
                mEnemyPoolDic[name] = new SimpleObjectPool<EnemyController>(() =>
                {
                    var isSuccess =   mEnemyPrefabDic.TryGetValue(name, out var go);
                    if(!isSuccess)
                    {
                        go = mEnemyPrefabDic.First().Value;
                    }
                    var newGo= go.Instantiate()
                                          .Name(name);
                    var enemyController=newGo.GetComponent<EnemyController>();
                   
                    return enemyController;

                }, go =>
                {
                    go.Hide();
                });
                
            enemyController= mEnemyPoolDic[name].Allocate();
           
            }
            else
            {
                enemyController= mEnemyPoolDic[name].Allocate();
            }
           
           
            return enemyController;  
        }
        /// <summary>
        /// 根据名字回收对象
        /// </summary>
        /// <param name="go"></param>
        public void Recycle(EnemyController  enemyController)
        {
            var name=enemyController.gameObject.name;
            if (!mEnemyPoolDic.ContainsKey(name))
            {
                Debug.LogError("不存在这个对象池");
            }
            else
            {
                 mEnemyPoolDic[name].Recycle(enemyController);
            }
        }

        public Enemydata GetEnemyData(string name)
        {
            var enemyData = mEnemyDatas.EnemyDataList.FirstOrDefault(e => e.Name == name);
            
            if (enemyData == null)
            {
                enemyData = new Enemydata();
                enemyData.Name = name;
                enemyData.HP = 10;
                enemyData.MoveSpeed = 10;
                enemyData.ID = -1;
                return enemyData;
            }
            else
            {
                return enemyData;
            }
        }
    }
}

